{% extends "base.html" %}

{% block title %}Distributed Log - Messages{% endblock %}

{% if feed  %}
	{% block alternate %}
	<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="{{ feed }}" />
	{% endblock alternate %}
{% endif %}

{% block content %}
		<a href="#" id="show_filter" onclick="return false;">Show / Hide Filter Panel</a>				
		<div id="filter_panel" style="display:none;">
			<form action="/" method="get">
				<table>
					<tr>
						<td>IP Address:</td>
						<td><input type="text" name="ip_addr" id="ip_addr" size="15" value="{{ filter.logger_ip|escape }}"/></td>
					</tr>
					<tr>
						<td>Time:</td>
						<td>
							<input type="text" name="ts" id="ts" size="20" value="{{ filter.ts|escape }}"/>							
						</td>
					</tr>
					<tr>
						<td>Application Name:</td>
						<td><input type="text" name="app_name" id="app_name" size="30" value="{{ filter.app_name|escape }}"/></td>
					</tr>
					<tr>
						<td>Logger Name:</td>
						<td><input type="text" name="logger_name" id="logger_name" size="20" value="{{ filter.logger_name|escape }}"/></td>
					</tr>
					<tr>
						<td>Severity:</td>
						<td>
							<select name="severity" id="severity">
								<option value="-1">All</option>
								<option value="0" {% ifequal filter.severity 0 %}selected=�True�{% endifequal %}>TRACE</option>
								<option value="1" {% ifequal filter.severity 1 %}selected=�True�{% endifequal %}>DEBUG</option>
								<option value="2" {% ifequal filter.severity 2 %}selected=�True�{% endifequal %}>INFO</option>
								<option value="3" {% ifequal filter.severity 3 %}selected=�True�{% endifequal %}>WARN</option>
								<option value="4" {% ifequal filter.severity 4 %}selected=�True�{% endifequal %}>ERROR</option>
								<option value="5" {% ifequal filter.severity 5 %}selected=�True�{% endifequal %}>FATAL</option>
							</select>
						</td>
					</tr>
					<tr>
						<td>Show dev logs:</td>
						<td>
							<input type="checkbox" name="dev_mode" {% if filter.dev_mode %} checked="checked" {% endif %} />
						</td>
					</tr>
					<tr>
						<td colspan="2">
							<input type="submit" value="Filter" /> &nbsp;
							<input type="reset" value="Clear" /> &nbsp;
							<a href="#" id="show_save_filter" onclick="return false;">Save Filter</a> &nbsp; 
							<a href="#" id="hide_filter" onclick="return false;">Hide</a>
						</td>
					</tr>
				</table>
			</form>
			<br />			
			<div id="save_filter_panel" style="display:none;">
				<form action="/filters" method="post">
					Description (optional): <input type="text" name="filter_desc" id="filter_desc" size="50" /> &nbsp; <input type="submit" value="Save" />
				</form>
				<a href="#" id="cancel_save_filter" onclick="return false;">Cancel</a>
			</div>			
		</div>
		
		
        <h2>Log Messages</h2>
		{% if filter %}
			Filter: {{ filter.to_string }}
		{% endif %}
        <table cellpadding="3" cellspacing="3" border="0" width="100%" style="border: 1px solid;">
			<thead>
            <tr style="background-color: #FFFF00;">
				<th />
                <th>IP</th>
				<th>Time</th>
				<th>Application</th>
                <th>Logger</th>
                <th>Severity</th>
                <th>Message</th>
            </tr>
			</thead>
			<tbody>
            {% for message in messages %}
			<tr class='{% cycle row_even,row_odd %}'>
				<td style="width: 25p;">
					{% if message.stack_dump %}
						<a href="#" id="show_stack"
							onclick="window.open('/?msg={{ message.key }}', 'Message', 'scrollbars=1,width=800,height=500');; return false;"
						>
							<image alt="Show" border="0" src="/images/eye.png" />
						</a>
					{% endif %}
				</td>			
                <td style="width: 90px">{{ message.logger_ip }}</td>
				<td style="width: 200px">{{ message.timestamp|escape }}</td>
				<td style="width: 100px">{{ message.app_name|escape }}</td>
                <td style="width: 150px">{{ message.break_logger_name }}</td>
                <td style="width: 80px">{{ message.severity_name }}</td>
                <td>{{ message.message|escape }}</td>
            </tr>
            {% endfor %}
			</tbody>
        </table>
        
        <form onSubmit="return paging()">
	        <a href="/?page=0{{filter_str}}">&lt;&lt;</a>
	        <a href="/?page={{prev}}{{filter_str}}">&lt;</a>
	        Page <input type="text" id="page" value="{{page}}" style="width: 40px" /> of {{pages}}
	        <a href="/?page={{next}}{{filter_str}}">&gt;</a>
	        <a href="/?page={{pages}}{{filter_str}}">&gt;&gt;</a>
	    </form>
	</div>
	
	<script type="text/javascript">
		function isNumeric(str) {
	   		var validChars = "0123456789";
	   		var res = true;

	   		if (str.length == 0) return false;

	   		for (var i = 0; i < str.length && res == true; i++) {
	      		var c = str.charAt(i);
	      		if (validChars.indexOf(c) == -1) {
	         		res = false;
	         	}
	      	}
	   		return res;
	   	}
	
		function paging() {
			val = document.getElementById('page').value;
			if (isNumeric(val)) {
				window.location = "/?page="+val+"{{filter_str}}";
			}
			return false;
		}
	
		$(document).ready(function(){

			var posX = -1;
			var posY = -1;
			
			$("a#show_filter").click(function () {
				$("div#filter_panel").toggle("Blind");
				if (posX < 0) {
					posX = $('input#ts').position().left + 45;
					posY = $('input#ts').position().top + $('input#ts').outerHeight() - 3;
					$('input#ts').daterangepicker({
						posX: posX,
						posY: posY
					});					
				}
			});
			
			$("a#hide_filter").click(function () {
				$("div#filter_panel").toggle("Blind");
			});				
							
			$("a#show_save_filter").click(function () {
				$("div#save_filter_panel").toggle("Blind");
			});	

			$("a#cancel_save_filter").click(function () {
				$("div#save_filter_panel").toggle("Blind");
			});				
		});
	</script>
	
	
{% endblock content %}